package com.xy.blog.user.controller;

import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.springmvc.controller.BaseController;
import com.xy.blog.core.springmvc.controller.IBaseController;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.user.controller.model.req.UserReq;
import com.xy.blog.user.controller.model.req.query.UserQuery;
import com.xy.blog.user.mapstruct.UserConverter;
import com.xy.blog.user.service.IUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description: 用户Rest层
 * @date 2019/10/21 21:42
 */
@Api(value = "UserController",tags = "用户管理")
@RequestMapping("/v1/user")
@RestController
public class UserController extends BaseController implements IBaseController<UserQuery, UserReq> {

    @Autowired
    private IUserService userServiceImpl;

    @Autowired
    private UserConverter userConverter;


    @Override
    public Object pages(PageReq<UserQuery> req) throws BaseException {
        return getPageData(userConverter.bo2Vo(userServiceImpl.listPage(req)));
    }

    @Override
    public Object getById(Long id) throws BaseException {
        return getData(userServiceImpl.getUserById(id));
    }

    @Override
    public Object save(@Valid UserReq req) throws BaseException {
        userServiceImpl.saveUser(userConverter.req2Bo(req));
        return success();
    }

    @Override
    public Object update(@Valid UserReq req) throws BaseException {
        userServiceImpl.updateUser(userConverter.req2Bo(req));
        return success();
    }

    @Override
    public Object deleteById(Long id) {
        userServiceImpl.deleteById(id);
        return success();
    }
}
